package com.bai.localshop.controller.admin;

import com.bai.localshop.pojo.dto.*;
import com.bai.localshop.pojo.vo.RoleOptionVO;
import com.bai.localshop.pojo.vo.RolePageVO;
import com.bai.localshop.pojo.vo.RoleVO;
import com.bai.localshop.result.Result;
import com.bai.localshop.service.RoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/api/admin/roles")
@RequiredArgsConstructor
@Slf4j
public class RoleController {

    private final RoleService roleService;

    /**
     * 获取角色列表
     */
    @GetMapping("/list")
    public Result<List<RoleOptionVO>> listRole() {
        return Result.success(roleService.listRole());
    }

    /**
     * 分页查询角色
     */
    @GetMapping("/page")
    public Result<IPage<RolePageVO>> pageQuery(RolePageDTO requestParam) {
        return Result.success(roleService.pageQuery(requestParam));
    }

    /**
     * 根据ID查询角色(回显数据)
     */
    @GetMapping("/{id}")
    public Result<RoleVO> getById(@PathVariable Long id) {
        return Result.success(roleService.getById(id));
    }

    /**
     * 修改角色
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody RoleUpdateDTO requestParam) {
        roleService.update(requestParam);
        return Result.success("修改成功");
    }

    /**
     * 删除角色（如果角色下有用户，则不能删除）
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        roleService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 更新角色状态
     */
    @PutMapping("/update/status")
    public Result<String> updateStatus(@RequestBody RoleUpdateStatusDTO requestParam) {
        roleService.updateStatus(requestParam);
        return Result.success("状态更新成功");
    }

    /**
     * 新增角色
     */
    @PostMapping
    public Result<String> save(@RequestBody RoleSaveDTO requestParam) {
        roleService.saveRole(requestParam);
        return Result.success("新增成功");
    }
}